package com.test.service.impl;

import com.test.entity.Permission;
import com.test.entity.User;
import com.test.mapper.UserMapper;
import com.test.service.UserService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("userService")  //定义bean的名称
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 用户注册
     *
     * @param user
     */
    @Override
    public void userRegistry(User user) {
        //用户注册的业务,对用户密码进行加密
        //saltUtils工具类获取随机盐,之后储存到数据库中,获取5位随机盐,散次列为10次
        String salt = com.test.util.saltUtils.getSaltUtils(5);
        Md5Hash md5Password = new Md5Hash(user.getPassword(), salt, 10);
        user.setSalt(salt);
        user.setPassword(md5Password.toString());
        userMapper.userRegistry(user);
    }

    /**
     * 根据用户名查询用户信息
     *
     * @param userName
     * @return
     */
    @Override
    public User findUserByNmae(String userName) {
        User user = userMapper.findUserByNmae(userName);
        return user;
    }

    /**
     * 根据用户名查询用户的所有角色信息
     *
     * @param username 用户名称
     * @return
     */
    @Override
    public User findRolesByUserName(String username) {
        return userMapper.findRolesByUserName(username);
    }

    /**
     * 根据角色id ,查询角色对应的多个权限信息
     * @param roleId  角色信息
     * @return
     */
    @Override
    public List<Permission> findPermsByRoleId(Integer roleId) {
        return userMapper.findPermsByRoleId(roleId);
    }
}
